package com.jrx.anytxn.transaction.service.impl;

import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.constant.TransErrMsgConstant;
import com.jrx.anytxn.transaction.entity.AmLoanAccountInfo;
import com.jrx.anytxn.transaction.entity.AmLoanMainInfo;
import com.jrx.anytxn.transaction.mapper.ext.ExtAmLoanAccountInfoMapper;
import com.jrx.anytxn.transaction.service.ILoanAccountMainInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 贷款订单账户service 实现
 *
 * @author : zhenghaopu
 * 2019/10/28
 */
@Service
public class LoanAccountMainInfoServiceImpl implements ILoanAccountMainInfoService {

    private static final Logger logger = LoggerFactory.getLogger(LoanAccountMainInfoServiceImpl.class);

    @Autowired
    private ISegmentService segmentService;
    @Resource
    private ExtAmLoanAccountInfoMapper extAmLoanAccountInfoMapper;

    @Override
    public AmLoanAccountInfo getAmLoanAccountInfoByLoanIdAndCusId(String loanId, String customerId, String tenantId) throws TxnBizException {
        AmLoanAccountInfo loanAccountInfo = extAmLoanAccountInfoMapper.selectByLoanId(loanId,customerId,tenantId);
        if (loanAccountInfo == null) {
            logger.error("用户：{}没有订单号：{}的订单账户", customerId, loanId);
            throw new TxnBizException(TxnRespCode.NO_DATA.getCode(), TxnRespCode.NO_DATA.getUserTips());
        }
        return loanAccountInfo;
    }

    @Override
    public AmLoanAccountInfo buildAmLoanAccountInfo(AmLoanMainInfo loanMainInfo) throws TxnBizException {

        AmLoanAccountInfo loanAccountInfo = BeanMapping.copy(loanMainInfo, AmLoanAccountInfo.class);
        try {
            loanAccountInfo.setId(segmentService.getId(TransBizConstant.AM_LOAN_ACCOUNT_ID));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnBizException(TxnRespCode.ERROR.getUserTips());
        }
        loanAccountInfo.setCurrBalance(BigDecimal.ZERO);//余额
        loanAccountInfo.setPrincipalBalance(BigDecimal.ZERO);//	本金余额
        loanAccountInfo.setInterestBalance(BigDecimal.ZERO);//利息余额
        loanAccountInfo.setPenaltyBalance(BigDecimal.ZERO);//罚息余额
        loanAccountInfo.setCompoundBalance(BigDecimal.ZERO);//复利余额
        loanAccountInfo.setFeeBalance(BigDecimal.ZERO);//服务费余额
        loanAccountInfo.setRemainPrincipal(loanMainInfo.getLoanAmount());//剩余本金 所有未还本金（包含未抛部分） 贷款金额
        loanAccountInfo.setAccruedInterest(BigDecimal.ZERO);//已计提未抛利息
        loanAccountInfo.setAccruedPenalty(BigDecimal.ZERO);//已计提未抛罚息
        loanAccountInfo.setAccruedCompound(BigDecimal.ZERO);//已计提未抛复利
        loanAccountInfo.setAccountStatus(TransBizConstant.LOAN_STATUS_0); //状态
        loanAccountInfo.setCastTenor(TransBizConstant.ZERO); //已抛期数
        loanAccountInfo.setPayedTenor(TransBizConstant.ZERO);// 已还期数  初始为0
        loanAccountInfo.setWaiveInterestFlag(TransBizConstant.YES_FLAG);//停息标志	0 = 停息 1 = 正常计息
        loanAccountInfo.setWaiveFeeFlag(TransBizConstant.YES_FLAG);//停费标志	0 = 停费  1 = 正常计费
        loanAccountInfo.setCompensatoryFlag(TransBizConstant.NO_FLAG);// 代偿标识	0 = 否    1 = 是
        loanAccountInfo.setBalTransferFlag(TransBizConstant.NO_FLAG);//债权转移标识	0 = 否 1 = 是
        loanAccountInfo.setTotalCompensatoryTenor(TransBizConstant.ZERO);//总代偿期数 贷款时0
        loanAccountInfo.setContinueOverDueTenor(TransBizConstant.ZERO);//连续逾期期数	贷款时0
        loanAccountInfo.setTotalOverDueTenor(TransBizConstant.ZERO);//总逾期期数	贷款时0
        loanAccountInfo.setAccountingPhase(TransBizConstant.ACCOUNTING_PHASE_0);//会计状态 0 = 正常  1 = 逾期 8 = 表外 9 = 核销 贷款时0 3 = 可疑 4 = 损失
        loanAccountInfo.setNonAccrualsFlag(TransBizConstant.NON_ACCRUALS_FLA_0);
        loanAccountInfo.setLoanClassify(TransBizConstant.LOAN_CLASSIFY_0);//五级分类	0 = 正常 1 = 关注 2 = 次级  ?
        loanAccountInfo.setChargeoffFlag(TransBizConstant.NO_FLAG);//核销标志
        loanAccountInfo.setCompelCollectionFlag(TransBizConstant.NO_FLAG);//强制入催标识
        loanAccountInfo.setLoanType(loanMainInfo.getLoanType());//新增贷款类型
        loanAccountInfo.setDelqDays(0);
        loanAccountInfo.setNextPaymentDate(loanMainInfo.getFirstPaymentDate());
        return loanAccountInfo;
    }
}
